<?php
/**
 * Notes:
 * User: Administrator
 * Date: 2020/9/21
 * Time: 17:02
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace App\Http\Controllers;


use App\Http\model\CheckRefundStatus;
use App\Http\model\Getorder;
use App\Http\model\GetRefund;
use App\Http\model\Poll;
use App\Http\model\Send;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\model\Jky;
use Illuminate\Support\Facades\Log;

class JkyController extends Controller
{

    /**
     * 【吉客云接口】
     * author      孙凯
     * time        20200922
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $method = $_REQUEST['method'];
        $appkey = $_REQUEST['appkey'];
        $token = $_REQUEST['token'];
        $bizcontent = $_REQUEST['bizcontent'];
        $sign = $_REQUEST['sign'];
        $appSecret = "86426339e6ca4890ae060bc00ce39907";    //appSecret的值通过在菠萝派中申请应用获得

        if (empty($method) || empty($appkey) || empty($token) || empty($bizcontent) || empty($sign)) {
            return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '公共接口缺少必要参数']);
        }

        $unsign = $appSecret . "appkey" . $appkey . "bizcontent" . $bizcontent . "method" . $method . "token" . $token . $appSecret;

        $newsign = md5(strtolower($unsign));
//        echo $newsign;
//        Log::info('sign' . $sign);
        if ($sign != $newsign) {
            return response()->json(['code' => '40000', 'message' => 'Logical Error', 'subcode' => 'GSE.VERIFYSIGN_FAILURE', 'submessage' => '签名验证失败']);
        }
//        Log::info('sing没问题' . $newsign);
        if ($method == "Differ.JH.Business.GetOrder") {
//            下载订单
            $request = json_decode($bizcontent, true);
            $PageIndex = $request["PageIndex"] == 0 ? 0 : $request["PageIndex"] - 1;
            $PageSize = $request["PageSize"];

            $GetSkuids_data_Model = new Getorder();
            $GetSkuids_data = $GetSkuids_data_Model->GetSkuids_data();
//            dd($GetSkuids_data);
            $hanld = DB::table('orderdetail');
            $hanld = $hanld->whereNotIn('SKU_ID', $GetSkuids_data);
//dd($hanld);
//            if ($request['OrderStatus'] == 'JH_01') {
////                    dd(234);
//                $hanld->where('orStatus', '=', 0);//待付款
//            }
//            if ($request['OrderStatus'] == 'JH_02') {
//                $hanld->whereIn('orStatus', [1, 2, 3]);//已付款
//            }
//
//            if ($request['OrderStatus'] == 'JH_03') {
//                $hanld->where('exprestate', '=', 1);//已发货
//            }
//            if ($request['OrderStatus'] == 'JH_04') {
//                $hanld->whereIn('orStatus', [9]);//已完成=快递100签收
//            }
//
//            if ($request['OrderStatus'] == 'JH_05') {
//
//                $hanld->whereIn('Status', [50, 51, 52, 59, 60, 61, 62, 69, 70, 71, 72, 79]);//售后
//            }

            if (!empty($request['PlatOrderNo'])) {
                $PlatOrderNo = $request['PlatOrderNo'];
                $hanld->where('OIcode', '=', $PlatOrderNo);
            }
            if (!empty($request['StartTime'])) {
                $hanld->where('OrderTime', '>=', $request['StartTime']);
            }

            if (!empty($request['EndTime'])) {
                $hanld->where('OrderTime', '<=', $request['EndTime']);
            }


//            $hanld = $hanld->
//            where(function ($where) use ($request) {
//                if ($request['OrderStatus'] == 'JH_01') {
////                    dd(234);
//                    $where->where('orStatus', '=', 0);//待付款
//                }
//                if ($request['OrderStatus'] == 'JH_02') {
//                    $where->whereIn('orStatus', [1, 2, 3]);//已付款
//                }
//
//                if ($request['OrderStatus'] == 'JH_03') {
//                    $where->where('exprestate', '=', 1);//已发货
//                }
//                if ($request['OrderStatus'] == 'JH_04') {
//                    $where->whereIn('orStatus', [9]);//已完成=快递100签收
//                }
//
//                if ($request['OrderStatus'] == 'JH_05') {
//                    $where->whereIn('Status', [50, 51, 52, 59, 60, 61, 62, 69, 70, 71, 72, 79]);//售后
//                }
//
//                if (!empty($request['PlatOrderNo'])) {
//
//                    $PlatOrderNo = $request['PlatOrderNo'];
//                    $where->where('OIcode', '=', $PlatOrderNo);
//                }
//                if (!empty($request['StartTime'])) {
//
//                    $where->where('OrderTime', '>=', $request['StartTime']);
//                }
//
//                if (!empty($request['EndTime'])) {
//                    $where->where('OrderTime', '<=', $request['EndTime']);
//                }
//
//            })
            $hanld->orderByDesc('OrderTime');
            $numtotalorder = $hanld->count();
            $res = $hanld->offset($PageIndex)->limit($PageSize)->get()->toArray();
//            dd($res);
            $Getorder = new  Getorder();
            return $array = $Getorder->GetOrderModel($numtotalorder, $res);


        } elseif ($method == "Differ.JH.Business.Send") {

            //发货接口
            $request = json_decode($bizcontent, true);
            $Send = new Send();
            $res = $Send->syncChangesModel($request);
//            Log::info('RES=====', json_encode($res));
            return CheckRefundSendStatusModel($res);


        } elseif ($method == "Differ.JH.Business.CheckRefundStatus") {

            //查询订单是否有退款接口
            $request = json_decode($bizcontent, true);
            if (!isset($request['PlatOrderNo'])) {
                return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '缺少平台订单OrderID']);
            }
            $checkrefundstatusModel = new CheckRefundStatus();
            $OrderID = $request['PlatOrderNo'];
            $CheckRefundStatus = $checkrefundstatusModel->searchOrderStatusModel($OrderID);//查找订单状态
            return $checkrefundstatusModel->CheckRefundStatusModel($CheckRefundStatus);//返回数据


        } elseif ($method == "Differ.JH.Business.DownloadProduct") {
            //商品下载接口
            $request = json_decode($bizcontent, true);
            $jky = new jky();
            return $jky->DownloadProductModel($request);

        } elseif ($method == "Differ.JH.Business.SyncStock") {

            //库存同步接口
            $request = json_decode($bizcontent, true);

            if (empty($request["PlatProductID"]) || ($request['Quantity'] < 0)) {
                return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '缺少平台商品sku编号或库存数量参数不对']);
            }
            $jky = new Jky();
            $product = $jky->getproductModel($request['PlatProductID']);

            if ($product == null) {
                return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => "找不到商品编码" . $request['PlatProductID'] . "对应的商品"]);
            }

            $update = $jky->syncstockModel($request['PlatProductID'], $request['Quantity']);
            $stock = $request['Quantity'];
            if ($update == 1) {
                return response()->json(['code' => '10000', 'message' => 'SUCCESS', 'Quantity' => $stock]);
            }


        } elseif ($method == "Differ.JH.Business.GetRefund") {

            $result = json_decode($bizcontent, true);
            //货物退款接口
            if (empty($result['BeginTime']) || empty($result['EndTime']) || empty($result['PageIndex']) || empty($result['PageSize'])) {
                return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '调用参数缺失']);
            }
            $GetRefundModel = new GetRefund();
            $PageIndex = $result["PageIndex"];
            $PageSize = $result["PageSize"];
            $res = $GetRefundModel->GetRefundModel($result, $PageIndex, $PageSize);
            return $res;

        } else {
            return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '调用接口方法名有误，超出可选范围']);
        }

    }

    public function CheckOrdersStatus($status)
    {
        switch ($status) {
            case 11:
                return 11;//'JH_01'买家已经申请退款等待卖家同意
                break;
            case 12:
                return 12;//'JH_06';//卖家已退款
                break;
            case 13:
                return 13;//'JH_04';//卖家拒绝退款
                break;
            default:
                return 7;//'JH_07';//没有退款
                break;
        }
    }


}
